package com.springapp.mvc.dm;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by shangdong on 17/4/11.
 */
public class DmRunService {
    private ExecutorService queryProductService = Executors.newCachedThreadPool();

    private NewCountListener countListener;

    public DmRunService() {
    }

    public NewCountListener getCountListener() {
        return countListener;
    }

    public void setCountListener(NewCountListener countListener) {
        this.countListener = countListener;
    }

    public DmRunService(NewCountListener countListener) {
        this.countListener = countListener;
    }

    public void queryProduct(List<Product> productList,int runSize){
        int defaultRunSize = 2;
        if (runSize > defaultRunSize){
            for (int i = 0; i < runSize; i++) {
                DmConnClient dmConnClient = new DmConnClient();
                run1(dmConnClient, productList);
            }
        }else {
            for (int i = 0; i < defaultRunSize; i++) {
                DmConnClient dmConnClient = new DmConnClient();
                run1(dmConnClient, productList);
            }
        }
//        DmConnClient dmConnClient = new DmConnClient();
//        run1(dmConnClient,productList);
    }

    public void queryProductForTest(List<Product> productList,int runSize){
//        int defaultRunSize = 3;
//        if (runSize > defaultRunSize){
//            for (int i = 0; i < runSize; i++) {
//                DmConnClient dmConnClient = new DmConnClient();
//                run1(dmConnClient, productList);
//            }
//        }else {
//            for (int i = 0; i < defaultRunSize; i++) {
//                DmConnClient dmConnClient = new DmConnClient();
//                run1(dmConnClient, productList);
//            }
//        }
        DmConnClient dmConnClient = new DmConnClient();
        run1(dmConnClient,productList);
    }
//    private void run(DmConnClient dmConnClient,List<Product> productList){
//        for (Product product : productList) {
//            queryProductService.submit(()->{
//                while (true){
//                    long l = System.currentTimeMillis();
//                    String count = dmConnClient.getCount(product.getProductUrl());
//                    long l1 = System.currentTimeMillis();
//                    System.out.println(l1 - l);
//                    int countInt =dmConnClient.parseInt(count);
//                    if (StringUtils.isNotBlank(count) && countInt > 0){
//                        String finalCount;
//                        if (countInt >= product.getBuyCount()){
//                            finalCount = String.valueOf(product.getBuyCount());
//                        }else {
//                            finalCount = count;
//                        }
//                        //todo add listener
//                        countListener.hasCount(finalCount,product);
//                    }
//                    long l2 = System.currentTimeMillis();
//                    System.out.println(l2-l1);
//                }
//            });
//        }
//    }

    private void run1(DmConnClient dmConnClient,List<Product> productList){
        for (Product product : productList) {
            queryProductService.submit(()->{
                int count = 0;
                while (true){
//                    long l = System.currentTimeMillis();
                    List<Integer> canChooseCount = dmConnClient.getCanChooseCount(product.getProductUrl());
//                    long l1 = System.currentTimeMillis();
//                    System.out.println("getChooseCount = "+ (l1 - l));
                    if (canChooseCount == null || canChooseCount.isEmpty()){
                        count ++;
                        if (count == 29){
                            System.out.println("查询商品无库存！"+ product.getProductUrl());
                            count = 0;
                        }
                    }else {
                        int finalCount = getFinalCount(product, canChooseCount);
                        System.out.println("此产品有库存 :"+product.getProductUrl() );
                        countListener.hasCount(String.valueOf(finalCount),product);
                    }
//                    long l2 = System.currentTimeMillis();
//                    System.out.println(l2-l1);
                }
            });
        }
    }

    public static void main(String[] args) {
        DmRunService ds = new DmRunService();
        Product product = new Product("https://www.dm.de/aptamil-pronutra-folgemilch-2-nach-dem-6-monat-p4008976022336.html", 28);
        DmConnClient dmConnClient = new DmConnClient();
        List<Integer> canChooseCount = dmConnClient.getCanChooseCount(product.getProductUrl());
        System.out.println(canChooseCount);
        int finalCount = ds.getFinalCount(product,canChooseCount);
        System.out.println(finalCount);
    }

    private int getFinalCount(Product product, List<Integer> canChooseCount) {
        int finalCount = 0;
        Collections.sort(canChooseCount);
        Integer max = canChooseCount.get(canChooseCount.size() - 1);
        if (max > product.getBuyCount()){
            for (int i = 0; i < canChooseCount.size(); i++) {
                if (i == canChooseCount.size() - 1){
                    finalCount = max;
                    break;
                }
                if (canChooseCount.get(i) <= product.getBuyCount() && canChooseCount.get(i+1) > product.getBuyCount()){
                    finalCount = canChooseCount.get(i);
                    break;
                }
            }
        }else {
            finalCount = max;
        }
        return finalCount;
    }

}
